<!-- 洗衣错衣 -->
<template>
  <div>
    <el-card style="width: 98%; margin: 0 auto; margin-top: 10px">
      <el-form ref="ruleFormRef" :model="form" label-width="auto" :inline="true">
        <el-form-item label="发布人:" prop="elderlyName">
          <el-input v-model="form.elderlyName" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="上报时间:" prop="beginDate">
          <el-date-picker
            v-model="value"
            type="daterange"
            clearable
            unlink-panels
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            @change="datachange"
          />
        </el-form-item>
        <el-form-item label="类型:" prop="type">
          <el-select v-model="form.type" placeholder="请选择" style="width: 200px" clearable>
            <el-option label="缺衣" value="缺衣" />
            <el-option label="错衣" value="错衣" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态:" prop="state">
          <el-select v-model="form.state" placeholder="请选择" style="width: 200px" clearable>
            <el-option label="未解决" value="未解决" />
            <el-option label="已解决" value="已解决" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search">查询</el-button>
          <el-button :icon="Refresh" @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="width: 98%; margin: 0 auto; margin-top: 10px">
      <ELTable v-bind="tableConfig" ref="clothesTable">
        <template #btns="{ deleteAll }">
          <el-button type="success" @click="add">
            <el-icon class="icon">
              <Plus />
            </el-icon>
            添加洗衣错衣
          </el-button>
          <el-button type="danger" @click="deleteAll">
            <el-icon class="icon">
              <Delete />
            </el-icon>
            批量删除
          </el-button>
        </template>

        <template #operate="{ scope, deleteHandle }">
          <el-button
            :icon="Tickets"
            text
            size="small"
            class="btn"
            @click="handelDetails(scope.row.id)"
          >
            详情
          </el-button>
          <el-button :icon="Edit" text size="small" class="btn" @click="uploadEdit(scope.row.id)">
            编辑
          </el-button>
          <el-button
            :icon="Delete"
            text
            size="small"
            class="btn"
            @click="deleteHandle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </ELTable>
    </el-card>

    <!-- 添加 -->
    <clothesDialog v-if="dialogVisible" :listParams="listParams" @handelClose="handelClose" />
    <!-- 详情 -->
    <clothesItemDialo
      v-if="dialogVisibleItem"
      :itemId="itemId"
      @handelCloseItem="handelCloseItem"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { Search, Refresh, Delete, Edit, Tickets } from "@element-plus/icons-vue";
import { FormInstance } from "element-plus";
import ELTable, { TableData } from "@/components/ELTable.vue";
import { clothesDel, clothesDelAll, clothesList } from "@/api/care/clothes/clothes";
import clothesDialog from "@/components/care/clothesAdd.vue";
import clothesItemDialo from "@/components/care/clothesItem.vue";
import { clothesListParams } from "@/api/care/clothes/type";

const dialogVisible = ref(false); //添加弹框
const dialogVisibleItem = ref(false); //详情弹框DF

const value = ref([]);
const form = reactive<clothesListParams>({
  page: 1,
  pageSize: 10,
  name: "",
  elderlyName: "",
  state: "",
  beginDate: "",
  endDate: "",
  type: "",
});

// 日期
const datachange = (e: any) => {
  // console.log(e);
  if (e != null) {
    form.beginDate = e[0];
    form.endDate = e[1];
  } else {
    form.beginDate = "";
    form.endDate = "";
  }
};

// 添加
const add = () => {
  dialogVisible.value = true;
};

// 编辑
let listParams = ref(0);
const uploadEdit = (id: number) => {
  listParams.value = id;
  dialogVisible.value = true;
};

// 详情
const itemId = ref(0);
const handelDetails = (id: number) => {
  itemId.value = id;
  dialogVisibleItem.value = true;
};

// 搜索
const search = () => {
  form.page = 1;
  clothesTable.value.getData(form);
};

// 重置
const ruleFormRef = ref<FormInstance>();
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  value.value = [];
};

// 关闭添加弹框
const clothesTable = ref();
const handelClose = () => {
  dialogVisible.value = false;
  clothesTable.value.getData();
};
// 关闭详情弹框
const handelCloseItem = () => {
  dialogVisibleItem.value = false;
};

// 表格
const tableConfig = reactive<TableData>({
  loadData: clothesList,
  delete: clothesDel,
  deleteAll: clothesDelAll,
  columns: [
    {
      prop: "id",
      label: "序号",
      attrs: {
        width: "70px",
      },
    },
    {
      prop: "elderlyName",
      label: "发布人",
    },
    {
      prop: "addTime",
      label: "发布时间",
    },
    {
      prop: "type",
      label: "类型",
    },
    {
      prop: "title",
      label: "标题",
    },
    {
      prop: "state",
      label: "状态",
    },
    {
      prop: "operate",
      label: "操作",
      attrs: {
        width: "250px",
      },
      slot: true,
    },
  ],
});
</script>

<style scoped lang="less">
.btn {
  color: rgb(64, 158, 255);
}

.btn:nth-child(3) {
  color: rgb(245, 108, 127);
}
</style>
